//
//  EGHomeModel.m
//  Orchard
//
//  Created by 黄勇锋 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGHomeModel.h"
#import "NetworkTool.h"

@interface EGHomeModel()

@property (strong,nonatomic) NSMutableArray *dataArray;

@end


@implementation EGHomeModel

+ (instancetype)modelWithDict:(NSDictionary *)dict{
    
    EGHomeModel *model = [[EGHomeModel alloc]init];
    
    [model setValuesForKeysWithDictionary:dict];
    
    return model;
}


- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

}


+ (void)getAdDataWithSuccess:(void(^)(NSArray *))success failure:(void(^)(NSError *))failure{
    
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/focus.json.php";
    
    NSDictionary *params = @{@"call":@"1"};
    
    [[NetworkTool shareManager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSDictionary *data = responseObject[@"data"];
    
        NSArray *focus = data[@"focus"];
        
        NSMutableArray *focusArray = [NSMutableArray array];
        
        [focus enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            EGHomeModel *model = [EGHomeModel modelWithDict:obj];
            
            [focusArray addObject:model];
        }];
        
        success(focusArray.copy);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
}


+ (void)getIconDataWithSuccess:(void(^)(NSArray *))success failure:(void(^)(NSError *))failure{
    
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/focus.json.php";
    
    NSDictionary *params = @{@"call":@"1"};
    
    [[NetworkTool shareManager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSDictionary *data = responseObject[@"data"];
        
        NSArray *icons = data[@"icons"];
        
        NSMutableArray *iconsArray = [NSMutableArray array];
        
        [icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            EGHomeModel *model = [EGHomeModel modelWithDict:obj];
            
            [iconsArray addObject:model];
        }];
        
        success(iconsArray.copy);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
    }];
}

+ (void)getActivitiesDataWithSuccess:(void(^)(NSArray *))success failure:(void(^)(NSError *))failure{
    
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/focus.json.php";
    
    NSDictionary *params = @{@"call":@"1"};
    
    [[NetworkTool shareManager] POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSDictionary *data = responseObject[@"data"];
        
        NSArray *activities = data[@"activities"];
        
        NSMutableArray *activitiesArray = [NSMutableArray array];
        
        [activities enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            EGHomeModel *model = [EGHomeModel modelWithDict:obj];
            
            [activitiesArray addObject:model];
        }];
        
        success(activitiesArray.copy);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
    }];
}




//- (NSMutableArray *)dataArray{
//    
//    if (nil == _dataArray) {
//        
//        _dataArray = [NSMutableArray array];
//    }
//    return _dataArray;
//}


@end
